<?php
function delete_space_tag($string)
{
    $string = mb_ereg_replace("\n", "", $string);
    $string = mb_ereg_replace("\r\n", "", $string);
    $string = mb_ereg_replace("\r", "", $string);
    $string = mb_ereg_replace(",", "", $string);
    return $string ;
}

function get_category_id($product_id)
{
    $query1 = mysql_query("select * from products_to_categories where products_id = '".$product_id."'");
    if(mysql_num_rows($query1)){
        $category_id = '';
        $i=0;
        while($result = mysql_fetch_array($query1)){
            $i++;
            $category_id .= $result['categories_id']; 
            
            if(mysql_num_rows($query1) != $i ){
                $category_id .= '/' ;
            }
        }
        return $category_id ;
    }
}

function get_special_price($product_id)
{
    $query2 = mysql_query("select specials_new_products_price from specials where products_id = '".$product_id."' and status = 1");
    if(mysql_num_rows($query2)){
        $result2 = mysql_fetch_array($query2);
        return $result2['specials_new_products_price'];
    }
}

function size($fileSize)
{
    $size = sprintf('%u', $fileSize);
    if($size == 0) {
        return('0B');
    }
    $sizename = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    return round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizename[$i];
}

function db_count($db)
{
   $query = mysql_query("select count(*) from ".$db);
   $row = mysql_fetch_array($query);
   return number_format($row['count(*)']);
}


function get_categories_name($cid,$langauges_id) {
    if($cid == "" || $cid == "0") {
        $string = "";
    } else {
        $query = mysql_query("select categories_name from categories_description where categories_id = '".$cid."' and language_id = '".$langauges_id."'");
        $result = mysql_fetch_array($query);
        
        $string = $result['categories_name'];
    }
    return $string;
}

function tep_get_product_path($id) 
{
    $cPath = '';
    
    $cat_count_sql = mysql_query("select count(*) as count from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$id . "'");
    $cat_count_data = mysql_fetch_array($cat_count_sql);
    
    if ($cat_count_data['count']) {
        $categories = array();
        
        $cat_id_sql = mysql_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$id . "'");
        $cat_id_data = mysql_fetch_array($cat_id_sql);
        tep_get_parent_categories($categories, $cat_id_data['categories_id']);
        
        $size = sizeof($categories)-1;
        for ($i = $size; $i >= 0; $i--) {
            if ($cPath != '') $cPath .= '_';
            $cPath .= $categories[$i];
        }
        if ($cPath != '') $cPath .= '_';
        $cPath .= $cat_id_data['categories_id'];
    }
    
    return $cPath;
}

function tep_get_parent_categories(&$categories, $categories_id)
{
    $parent_categories_query = mysql_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$categories_id . "'");
    while ($parent_categories = mysql_fetch_array($parent_categories_query)) {
      if ($parent_categories['parent_id'] == 0) return true;
          $categories[sizeof($categories)] = $parent_categories['parent_id'];
          if ($parent_categories['parent_id'] != $categories_id) {
            tep_get_parent_categories($categories, $parent_categories['parent_id']);
          }
    }
}

function chenge_block($value)
{
    $res = str_replace(array("\n", "\r", "\t", "\\",","), array("", "", "", "",""), $value);
    return $res;
}
  
function get_lang_list()
{
  $query = mysql_query("select * from languages order by sort_order asc");
  $string = '<select name="lang">';
  while($result = mysql_fetch_array($query)){
      $string .= '<option value="'.$result['languages_id'].'">'.$result['name'].'</option>';
  } 
  $string .= '</select>';
  
  return $string ;
}

function tep_get_zone_name($zone_id) 
{
    $zone_query = mysql_query("select zone_name from " . TABLE_ZONES . " where zone_id = '" . $zone_id . "'");

    if (!mysql_num_rows($zone_query)) {
      return $zone_id;
    } else {
      $zone = mysql_fetch_array($zone_query);
      return $zone['zone_name'];
    }
  }

function tep_get_country_name($country_id) {
    $country_query = mysql_query("select countries_name from " . TABLE_COUNTRIES . " where countries_id = '" . (int)$country_id . "'");

    if (!mysql_num_rows($country_query)) {
     return $country_id;
    } else {
      $country = mysql_fetch_array($country_query);
      return $country['countries_name'];
    }
}
